package com.etc.controller;

import com.etc.entity.Business;
import com.etc.entity.Goods;
import com.etc.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

// 信息管理类
@Controller
@RequestMapping("/b")
public class BusinessController {
	@Autowired
    BusinessService businessService;
  
  
	
    // 显示主页
    @RequestMapping("/queryBusiness1")
    public ModelAndView querysBusiness1(){
    	ModelAndView mv  = new ModelAndView();
        mv.setViewName("index2");
    return mv;
    }
    
    // 查询信息类，page为页码
    @RequestMapping("/queryBusiness2/{page}")
    public ModelAndView querysBusiness2(@PathVariable("page") int page){
      List<Business> list=  businessService.queryBusiness(page);
        ModelAndView mv  = new ModelAndView();
        mv.addObject("list",list);
        int totalCount = businessService.getAll();
        // 计算总页数
        int pageCount = totalCount / 5;
        if ( totalCount % 5 != 0) {
        	pageCount++;
        }


        // 生成页数列表
        List<Integer> pageList = new ArrayList<Integer>();
        for (int i = 0 ;i < pageCount; i++) {
        	pageList.add(i);
        }
        // 存入model中
        mv.addObject("pageList", pageList);
        mv.addObject("totalCount", businessService.getAll());
        mv.setViewName("Business_Manage");
        
    return mv;
    }
    
    //查询id的管理类
    @RequestMapping("/queryBusiness3/{id}")
    public ModelAndView querysBusiness3(@PathVariable("id") int id){
    	// 获取business类
      Business u=  businessService.queryBusiness3(id);
     
      // 存入model中
        ModelAndView mv  = new ModelAndView();
        mv.addObject("Business",u);
        // 设置返回视图
        mv.setViewName("Business_Edit");
    return mv;
    }



    // 更新Business类信息
    @RequestMapping("/update")
    public ModelAndView updateUser(Business u){
    	// 更新business类试题
    	businessService.updateBusiness(u);
     
    	return this.querysBusiness2(0);
    }
    
    
    
    @RequestMapping("/queryBusinessFilter")
    public ModelAndView queryBusinessFilter(int page, String name){
    	System.out.println(page);
      List<Business> list=  businessService.queryBusinessFilter(page, name);
     
        ModelAndView mv  = new ModelAndView();
        mv.addObject("list",list);
        int totalCount = businessService.queryAllBusinessFilter(name);
        int pageCount = totalCount / 5;
        if ( totalCount % 5 != 0) {
        	pageCount++;
        }
        
        List<Integer> pageList = new ArrayList<Integer>();
        for (int i = 0 ;i < pageCount; i++) {
        	pageList.add(i);
        }
        mv.addObject("pageList", pageList);
        mv.addObject("totalCount", businessService.getAll());
        
        mv.setViewName("Business_Manage");
        
    return mv;
    }
    


    /**
     *删除指定商家
     */
    @RequestMapping("/delete/{id}")
    public ModelAndView deleteBusiness(@PathVariable("id") int id){
    	// 删除指定id的business类
    	businessService.deleteBusiness(id);
    	return this.querysBusiness2(0);
    }



    /**
     * 查询商品信息
     */
    @RequestMapping("/queryBusiness4/{id}")
    public ModelAndView querysBusiness4(@PathVariable("id") int id){
        List<Goods> list=  businessService.selectGoods(id);
        System.out.println(list.get(0).getGoodsId());
        ModelAndView mv  = new ModelAndView();
        mv.addObject("list",list);
        mv.setViewName("Goods_Manage");
        return mv;
    }



    /**
     * 修改商品状态类
     * 修改显示状态，如果等于0（下架）就变成1（上架），反之亦然
     */
    @GetMapping("/updatebus")
    public ModelAndView updateGoods(int id, int groundingId,int busid){
        System.out.println(id);
        System.out.println(groundingId);
        System.out.println(busid);
        groundingId = groundingId == 0 ? 1 : 0;
        System.out.println(groundingId);
        int i = businessService.updateStatus(id, groundingId);
        if (i>0){
            System.out.println("success");
        }else{
            System.out.println("error");
        }
        return this.querysBusiness4(busid);
    }

    
}
